/*
 * Spring 2012 TCSS360 Deliverable 3
 * Matthew Selfors
 */

package gui;

import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import javax.swing.JOptionPane;

import model.Auction;
import model.Bidder;
import model.CalendarModel;
import model.NPOStaffMember;
import model.UserAccess;
import storage.ProgramStateHandler;

@SuppressWarnings("serial")
public class LoginGUI extends JOptionPane {

	private String username;

	private UserAccess user_access;

	private CalendarModel calendar_model;

	private AuctionGUI auction_panel;

	private AuctionFormGUI auction_form_panel;

	private ProgramStateHandler my_ps;

	public LoginGUI(final CalendarModel the_calendar_model,
			final UserAccess the_user_access,
			final ProgramStateHandler ps) {
		super();
		user_access = the_user_access;
		calendar_model = the_calendar_model;
		my_ps = ps;

		while (!user_access.getUsers()
				.containsKey(username)) {
			username =
					showInputDialog(null, "Username: ",
							"Login", PLAIN_MESSAGE);
			if (username == null) {
				break;
			}
			checkInput();
		}

		if (username != null) {
			calendar_model = the_calendar_model;

			if (user_access.getUsers().get(username)
					.getAccessLevel() == 1) {

				new CalendarGUI(calendar_model, user_access
						.getUsers().get(username), my_ps,
						user_access);
			} else if (user_access.getUsers().get(username)
					.getAccessLevel() == 2) {
				String npo =
						((NPOStaffMember) user_access
								.getUsers().get(username))
								.getNPO();
				java.util.Iterator<Entry<Date, List<Auction>>> it =
						calendar_model
								.getAllCurrentAuctions()
								.entrySet().iterator();
				while (it.hasNext()) {
					List<Auction> a_list =
							it.next().getValue();
					for (int i = 0; i < a_list.size(); i++) {
						if (a_list.get(i).getNPO()
								.equals(npo)) {
							auction_panel =
									new AuctionGUI(
											a_list.get(i),
											user_access
													.getUsers()
													.get(username));
							auction_panel.start(my_ps,
									calendar_model,
									user_access);
							return;
						}
					}
				}
				auction_form_panel =
						new AuctionFormGUI(
								calendar_model,
								my_ps,
								user_access,
								(NPOStaffMember) user_access
										.getUsers().get(
												username));
				auction_form_panel.start();
			} else if (user_access.getUsers().get(username)
					.getAccessLevel() == 3) {
				new AuctionListGUI(calendar_model,
						(Bidder) user_access.getUsers()
								.get(username), my_ps,
						user_access);
			}
		}
	}

	private void checkInput() {
		if (!user_access.getUsers().containsKey(username)) {
			showMessageDialog(
					null,
					"User does not exist; please enter a valid username.",
					"Invalid Username!",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	public String getUsername() {
		return username;
	}
}
